package com.atgugu.realtime.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/12/25 9:57
 */
public class AtguiguUtil {
    public static <T> List<T> toList(Iterable<T> it) {
        ArrayList<T> result = new ArrayList<>();
        it.forEach(result::add);
        return result;
    }
    
    public static Long toTs(String dateTime, String... dateFormat) {
        String format = "yyyy-MM-dd HH:mm:ss";
        if (dateFormat.length > 0) {
            format = dateFormat[0];
        }
        
        try {
            return new SimpleDateFormat(format).parse(dateTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static String toDataTime(long ts, String... dateFormat) {
        String format = "yyyy-MM-dd HH:mm:ss";
        if (dateFormat.length > 0) {
            format = dateFormat[0];
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));// 使用指定的时区
        return sdf.format(ts);
    }
}
